
package GUI;

/**
 * @author David Ponce
 * @author Philipp Armbruster
 */

import Interaction.ServerConnection;
import Flightadministration.Flightadministration;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class PassengerCreate extends JPanel {

    final Main td;
    private JPanel jp;
    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();


    // Kosntruktor
    public PassengerCreate(final Main td) throws RemoteException {
        
        this.td = td;
        showPassengerCreate();
    }

    
    // Methoden
    public void showPassengerCreate(){

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************
        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 1;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren

        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Anlegen eines Passagiers</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        JLabel label3 = new JLabel("Nachname: ");
        constraints.gridx = 0;
        constraints.gridy = 1;
        jp.add(label3, constraints);

        final JTextField textfield2 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 1;
        jp.add(textfield2, constraints);

        JLabel label4 = new JLabel("Vorname: ");
        constraints.gridx = 0;
        constraints.gridy = 2;
        jp.add(label4, constraints);

        final JTextField textfield3 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 2;
        jp.add(textfield3, constraints);

        JLabel label5 = new JLabel("Straße: ");
        constraints.gridx = 0;
        constraints.gridy = 3;
        jp.add(label5, constraints);

        final JTextField textfield4 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 3;
        jp.add(textfield4, constraints);

        JLabel label6 = new JLabel("Ort: ");
        constraints.gridx = 0;
        constraints.gridy = 5;
        jp.add(label6, constraints);

        final JTextField textfield5 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 5;
        jp.add(textfield5, constraints);

        JLabel label7 = new JLabel("PLZ: ");
        constraints.gridx = 0;
        constraints.gridy = 4;
        jp.add(label7, constraints);

        final JTextField textfield6 = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 4;
        jp.add(textfield6, constraints);


        //Buttons
        //**********************************************************************

        JButton button_CreatePassenger = new JButton("Passagier anlegen");
        constraints.gridx = 0;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        jp.add(button_CreatePassenger, constraints);

        JButton button_Cancel = new JButton("Zurück zur Übersicht");
        constraints.gridx = 1;
        constraints.gridy = 6;
        constraints.gridwidth = 1;
        jp.add(button_Cancel, constraints);


        //Action Listener
        //**********************************************************************

        button_CreatePassenger.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                if ((textfield2.getText().equals("")) ||
                    (textfield3.getText().equals("")) ||
                    (textfield4.getText().equals("")) ||
                    (textfield5.getText().equals("")) ||
                    (textfield6.getText().equals("")))
                {
                    JOptionPane.showMessageDialog(null, "Bitte alle Felder ausfüllen!");
                }
                else {
                    try {
                        String currentPLZ = textfield6.getText();

                        if(currentPLZ.length() != 5) {
                            JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                                "Die Postleitzahl muss immer 5-stellig sein,<br>" +
                                                                "bitte korregieren Sie ihre Eingabe!</p></html>");
                        }
                        else {

                            int currentPLZAsInt = Integer.parseInt(currentPLZ);

                            // Passagier anlegen
                            admin.createPassenger(textfield3.getText(), textfield2.getText(), textfield4.getText(),
                                                    currentPLZAsInt, textfield5.getText());
                                // JTables aktualisieren
                                td.updatePassenger();

                            JOptionPane.showMessageDialog(null, "Passagier angelegt");
                            textfield2.setText("");
                            textfield3.setText("");
                            textfield4.setText("");
                            textfield5.setText("");
                            textfield6.setText("");

                            ((CardLayout) td.rightp.getLayout()).show(td.rightp, "PassengerM");
                        }

                    } catch (NumberFormatException ex) {
                        JOptionPane.showMessageDialog(null, "<html><p style=\"color:red; text-align: center\">" +
                                                            "Die Postleitzahl muss ausschließlich aus Zahlen bestehen!" +
                                                            "</p></html>");
                        textfield6.setText("");
                    } catch (RemoteException ex) {
                        Logger.getLogger(AircraftCreate.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });


        button_Cancel.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){

                    ((CardLayout) td.rightp.getLayout()).show(td.rightp,"PassengerM");
            }
        });

    }

    public JPanel getJp() {
        return jp;
    }
}


